package org.gege.crawler

//java和scala相关语法转换
import scala.collection.JavaConversions._
import scala.util.Try
import org.jsoup.Jsoup
import scala.util.Failure
import scala.util.Success
import sun.net.www.http.HttpClient
import com.alibaba.fastjson.JSON
import java.io.PrintWriter
import java.io.File

class AutoHome {
  val home = "http://club.autohome.com.cn/JingXuan/106/%d";

  var topicUrl = "http://club.autohome.com.cn/JingXuan/GetTopicClicksByTopicIds?t=0.44239072878013&ids=%s"

  val topicPattern = ".+-.+-.+-(\\d+)-.+.html.+".r

  def itemList(pageNo: Int): List[ListItem] = {
    var map: Map[Long, ListItem] = Map()

    var topicIds = "";

    var url = home.format(pageNo);
    println(url)
    Try(Jsoup.connect(url).get()) match {
      case Failure(e) => //抛错
        if (pageNo > 40) {
        } else throw e

      case Success(d) => //正常
        var ul = d.select("ul.content").get(0)

        var els = ul.select("li")
        for (li <- els) {
          var divPicBox = li.select("div.pic-box").get(0)
          var divPicTxt = li.select("div.pic_txt").get(0)

          var item = new ListItem

          item.imgUrl = divPicBox.select("a > img").get(0).attr("src")
          item.link = divPicBox.select("a").get(0).attr("href")
          item.title = divPicTxt.select("p > a").get(0).html
          item.topicId = topicId(item.link)

          var userDom = divPicTxt.select("dt.user").get(0)
          item.userLink = userDom.select("a").attr("href")
          item.userLogoImgUrl = userDom.select("img").get(0).attr("src")
          item.userName = userDom.select("span").html

          topicIds += item.topicId + ","
          map = map + (item.topicId -> item)
        }
    }

    var list: List[ListItem] = List()

    if (topicIds.length() > 3) {

      Try(Jsoup.connect(topicUrl.format(topicIds)).get()) match {
        case Failure(e) => //抛错
          if (pageNo > 40) {
          } else throw e

        case Success(d) => //正常
          var str = d.select("body").get(0).html
          str = str.replaceAll("&quot;", "")
          var topics = JSON.parseArray(str, classOf[TopicBean])
          var t = new TopicBean

          for (b <- topics) {
            var lItemOp = map.get(b.getTopicId)
            if (lItemOp.nonEmpty) {
              var lItem = lItemOp.get
              lItem.read = b.getViews
              lItem.replys = b.getReplys

              list = list.+:(lItem)
            }
          }
      }
    }

    list
  }

  def topicId(url: String): Long = {
    var arr = topicPattern.findAllMatchIn(url)
    for (r <- arr) {
      return r.group(1).toLong
    }

    0
  }
}

object AutoHome {

  def main(args: Array[String]): Unit = {
    var auto = new AutoHome

    var path = "E:/temp/scala/auto.txt";
    val writer = new PrintWriter(new File(path))
    for (i <- 1 to 10) {
      var list = auto.itemList(i)

      for (item <- list) {
        writer.println(item.txtInfo)
      }

      i.+(1)
    }
    writer.flush()
    writer.close()
  }

}